// const { post } = require("../../../utils/http");
import request from '../../../utils/http.js'
// pkg_wo/pages/faPiao/faPiao.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    isSQBack:false,//判断是否是“立即开票”选好订单后的返回
    personId: 0,
    keKaiPiaoJinE:0,
    type: '',
    title: '',
    taxNumber: '',
    companyAddress: '',
    telephone: '',
    bankName: '',
    bankAccount: '',


    userName: '', //联系人
    provinceName: '', //省
    cityName: '',
    countyName: '', //区
    detailInfo: '', //详细地址
    telNumber: '', //联系电话
    addressStr: '',
    eMail: '',
    faPiaoList: ['增值税普通发票'],
    faPiaoIndex: 0,

    faPiaoSQList: [], //已提交的发票申请
    orderFormIdList:[], //订单iDlist
    orderListStr:'',
    // paperOreleIndex:0,//0电子  1纸质
    // paperOreleList:['电子发票']

    checkIsReady: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      personId: options.personId,
      type: wx.getStorageSync('type') == '' ? 1 : wx.getStorageSync('type'),
      // faPiaoList: wx.getStorageSync('type') == '0' ? ['增值税普通发票', '增值税专用发票'] : ['增值税普通发票'],
      title: wx.getStorageSync('title'),
      taxNumber: wx.getStorageSync('taxNumber'),
      companyAddress: wx.getStorageSync('companyAddress'),
      telephone: wx.getStorageSync('telephone'),
      bankName: wx.getStorageSync('bankName'),
      bankAccount: wx.getStorageSync('bankAccount'),
      userName: wx.getStorageSync(app.globalData.USERNAME), //联系人
      provinceName: wx.getStorageSync(app.globalData.PROVINCENAME), //省
      cityName: wx.getStorageSync(app.globalData.CITYNAME),
      countyName: wx.getStorageSync(app.globalData.COUNTYNAME), //区
      detailInfo: wx.getStorageSync(app.globalData.DETAILINFO), //详细地址
      telNumber: wx.getStorageSync(app.globalData.TELNUMBER), //联系电话

      addressStr: wx.getStorageSync(app.globalData.PROVINCENAME) + wx.getStorageSync(app.globalData.CITYNAME) + wx.getStorageSync(app.globalData.COUNTYNAME) + wx.getStorageSync(app.globalData.DETAILINFO) + "  " + wx.getStorageSync(app.globalData.USERNAME) + "  " + wx.getStorageSync(app.globalData.TELNUMBER)

    })

    this.getAction();
    this.geteEmail();


  },


  //获取发票list
  getAction: function () {

    wx.showLoading({
      title: '获取中...',
      mask: true
    })
  
    var that = this

    request.get('faPiao/faPiaoLieBiao', null, res => {

     console.log("获取发票:" + JSON.stringify(res.data))
     that.setData({
      faPiaoSQList: res.data.list,
      keKaiPiaoJinE:res.data.keKaiPiaoJinE
    })

    wx.hideLoading()

    }, err => {

    
      wx.showToast({
        title: '获取数据失败,请检查网络', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 3000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })

    }, complete => {
      wx.hideLoading()

    })

  },

  //获取email
  geteEmail: function () {


    var that = this

    
    request.get('person/getEMail', null, res => {


      console.log("emai:" + JSON.stringify(res.data))
      that.setData({
        eMail: res.data.message
      })

      that.checkInfoIsReady();

     
     }, err => {
 
     
       wx.showToast({
         title: '获取数据失败,请检查网络', // 提示的内容
         icon: "none", // 图标，默认success
         duration: 3000, // 提示的延迟时间，默认1500
         mask: false, // 是否显示透明蒙层，防止触摸穿透
       })
 
     }, complete => {
      //  wx.hideLoading()
 
     })


  },

  //提交申请
  tijiaoAction: function () {


    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var that = this

    let data={
      shenQingRole: 1,
      yeWuId: that.data.personId,
      xingZhi: that.data.faPiaoIndex + 1,
      leiXing: that.data.type == 1 ? 3 : (that.data.faPiaoIndex == 0 ? 1 : 2),
      title: that.data.title,
      taxNumber: that.data.taxNumber,
      companyAddress: that.data.companyAddress,
      telephone: that.data.telephone,
      bankName: that.data.bankName,
      bankAccount: that.data.bankAccount,
      eMail: that.data.eMail,
      wxUserName: that.data.userName,
      wxProvinceName: that.data.provinceName,
      wxPostalCode: '',
      wxCityName: that.data.cityName,
      wxCountyName: that.data.countyName,
      wxDetailInfo: that.data.detailInfo,
      wxNationalCode: '',
      wxTelNumber: that.data.telNumber,
      orderFormIdList:that.data.orderListStr,

    }

    request.get('faPiao/shenQingFaPiao', data, res => {

      // console.log("提交:" + JSON.stringify(res.data))
      that.setData({
        isSQBack:false
      })

      if(res.data.status==1){
        that.getAction()
      }else{
        wx.showToast({
          title: '申请失败', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      }
 
     wx.hideLoading()
 
     }, err => {
 
     
       wx.showToast({
         title: '获取数据失败,请检查网络', // 提示的内容
         icon: "none", // 图标，默认success
         duration: 3000, // 提示的延迟时间，默认1500
         mask: false, // 是否显示透明蒙层，防止触摸穿透
       })
 
     }, complete => {
       wx.hideLoading()
 
     })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.checkInfoIsReady();
    if(this.data.isSQBack){//提交发票申请
       this.tijiaoAction();
    }
  },

  //检查发票开票信息是否完整
  checkInfoIsReady: function () {

    if (this.data.type == '' || this.data.title == '' || (this.data.faPiaoIndex == 0 && this.data.eMail == '') || (this.data.faPiaoIndex == 1 && this.data.detailInfo == '')) {

      this.setData({
        checkIsReady: false
      })
    } else {

      if (this.data.type == 0 && this.data.taxNumber == '') { //公司
        this.setData({
          checkIsReady: false
        })
      } else {
        this.setData({
          checkIsReady: true
        })
      }
    }

  },


  //去开票
  tofaPiaoAction: function () {
    if (this.data.checkIsReady) {

      wx.showModal({
        title: '温馨提示',
        content: '开票前，请确认开票信息是否准确！',
        showCancel: true,
        confirmText:'准确',
        cancelText:'返回',
        success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pkg_wo/pages/faPiaoAction/faPiaoAction',
              })
            }
        }
    })


    } else {
      wx.showToast({
        title: "请先完善开票信息！", // 提示的内容
        icon: "none", // 图标，默认success
        duration: 3000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    }

  },


  tofaPiaoInfo:function(e){
    var id=e.currentTarget.dataset.id
    var s=e.currentTarget.dataset.s
    wx.navigateTo({
      url: '../faPiaoInfo/faPiaoInfo?id='+id+'&s='+s,
    })

  },

  
  //发票信息
  setchooseInvoice: function () {

    var that = this

    wx.chooseInvoiceTitle({
      success: (result) => {
        // console.log("发票:" + JSON.stringify(result))

        wx.setStorageSync("type", result.type);
        wx.setStorageSync("title", result.title);
        wx.setStorageSync("taxNumber", result.taxNumber);
        wx.setStorageSync("companyAddress", result.companyAddress);
        wx.setStorageSync("telephone", result.telephone);
        wx.setStorageSync("bankName", result.bankName);
        wx.setStorageSync("bankAccount", result.bankAccount);
        that.setData({
          // faPiaoList: result.type == '0' ? ['增值税普通发票', '增值税专用发票'] : ['增值税普通发票'],
          faPiaoIndex: 0,
          type: result.type,
          title: result.title,
          taxNumber: result.taxNumber,
          companyAddress: result.companyAddress,
          telephone: result.telephone,
          bankName: result.bankName,
          bankAccount: result.bankAccount,
        })
        that.checkInfoIsReady();

      },
    })
  },


  //选择地址
  setAderssTap: function () {
    var that = this
    if (wx.chooseAddress) {
      wx.chooseAddress({
        success: function (res) {
          wx.setStorageSync(app.globalData.USERNAME, res.userName);
          wx.setStorageSync(app.globalData.PROVINCENAME, res.provinceName);
          wx.setStorageSync(app.globalData.CITYNAME, res.cityName);
          wx.setStorageSync(app.globalData.COUNTYNAME, res.countyName);
          wx.setStorageSync(app.globalData.DETAILINFO, res.detailInfo);
          wx.setStorageSync(app.globalData.TELNUMBER, res.telNumber);
          wx.setStorageSync(app.globalData.NATIONALCODE, res.nationalCode);
          wx.setStorageSync(app.globalData.POSTALCODE, res.postalCode);
          that.setData({
            userName: res.userName,
            provinceName: res.provinceName,
            cityName: res.cityName,
            countyName: res.countyName,
            detailInfo: res.detailInfo,
            telNumber: res.telNumber,
            nationalCode: res.nationalCode,
            postalCode: res.postalCode,
            addressStr: res.provinceName + res.cityName + res.countyName + res.detailInfo + "  " + res.userName + "  " + res.telNumber
          })
          that.checkInfoIsReady();
        },
        fail: function () {


          wx.openSetting({
            success: (res) => {
              if (res.authSetting["scope.address"]) { ////如果用户重新同意了授权
                wx.chooseAddress({
                  success: function (ress) {
                    wx.setStorageSync(app.globalData.USERNAME, ress.userName);
                    wx.setStorageSync(app.globalData.PROVINCENAME, ress.provinceName);
                    wx.setStorageSync(app.globalData.CITYNAME, ress.cityName);
                    wx.setStorageSync(app.globalData.COUNTYNAME, ress.countyName);
                    wx.setStorageSync(app.globalData.DETAILINFO, ress.detailInfo);
                    wx.setStorageSync(app.globalData.TELNUMBER, ress.telNumber);
                    wx.setStorageSync(app.globalData.NATIONALCODE, ress.nationalCode);
                    wx.setStorageSync(app.globalData.POSTALCODE, ress.postalCode);
                    that.setData({
                      userName: ress.userName,
                      provinceName: ress.provinceName,
                      cityName: ress.cityName,
                      countyName: ress.countyName,
                      detailInfo: ress.detailInfo,
                      telNumber: ress.telNumber,
                      nationalCode: ress.nationalCode,
                      postalCode: ress.postalCode,
                      addressStr: res.provinceName + res.cityName + res.countyName + res.detailInfo + "  " + res.userName + "  " + res.telNumber
                    })
                    that.checkInfoIsReady();
                  },
                })
              }
            }
          })
        }
      })


    } else {
      console.log('当前微信版本不支持chooseAddress');
    }

  },

  

  //设置电子邮箱
  setEmailTap: function () {
    wx.navigateTo({
      url: '../eMail/eMail?personId=' + this.data.personId,
    })
  },





  //选发票类型
  bindPickerChange(e) {
    // console.log("发票:" + JSON.stringify(e.detail.value))
    this.setData({
      faPiaoIndex: e.detail.value,
      // paperOreleList:e.detail.value==1?['电子发票','纸质发票']:['电子发票']
    })
    this.checkInfoIsReady();
  },

  //选电子or纸质
  bindpapereleChange(e) {
    this.setData({
      paperOreleIndex: e.detail.value,
    })
    this.checkInfoIsReady();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})